import Cache from "@/utils/cache.js"
const state = {
	userInfo: Cache.get('USERINFO') || {},
	token: Cache.get('TOKEN') || null,
};

const mutations = {
	login(state, opt) {
		state.token = opt.token;
		state.userInfo = opt
		Cache.set('TOKEN', opt.token);
		Cache.set('USERINFO', opt);
		// this.dispatch('getUser')
	},
	logout(state) {
		state.token = null;
		state.userInfo = {}
		Cache.remove('TOKEN');
		Cache.remove('USERINFO');
	},
	setUserInfo(state, user) {
		state.userInfo = user
	},
};

const actions = {
	getUser({
		state,
		commit
	}) {
		return new Promise(resolve => {
			getUser().then(res => {
				if (res.code == 1) {
					commit('setUserInfo', res.data)
				}
				resolve()
			})
		})
	},
};

export default {
	state,
	mutations,
	actions
};